<?php
#### PAGES MODEL ###############################################################
class PagesModel {

	#### CONSTRUCTOR ###########################################################
	function PagesModel() {
		Base::getThis();
	}

	#### GET PAGES #############################################################
	function getPages($limit = FALSE) {
		$this->db->select($this->db->table['pages']);
		$this->db->limit($limit);
		$this->db->query() or sql_error(__FILE__, __LINE__);
		return $this->db->result();
	}

	#### GET ENABLED PAGES #####################################################
	function getEnabledPages($limit = FALSE) {
		$this->db->select($this->db->table['pages']);
		$this->db->where('disabled = 0');
		$this->db->limit($limit);
		$this->db->query() or sql_error(__FILE__, __LINE__);
		return $this->db->result();
	}

	#### GET PAGES #############################################################
	function getPagesForForm($none = TRUE, $limit = FALSE) {
		$this->db->select($this->db->table['pages']);
		$this->db->limit($limit);
		$this->db->query() or sql_error(__FILE__, __LINE__);
		$pages = $this->db->result(TRUE);

		if ($none) $page_options[0] = 'None';
		foreach ($pages as $page) $page_options[$page['id']] = $page['title'];

		return $page_options;
	}

	#### GET PAGE BY ID ########################################################
	function getPageByID($id, $fields = FALSE) {
		$this->db->select($this->db->table['pages'], $fields).
		$this->db->where('id = '.$id);
		$this->db->query() or sql_error(__FILE__, __LINE__);
		return $this->db->fetchArray();
	}

	#### GET PAGE BY SEF #######################################################
	function getPageBySef($sef) {
		$this->db->select($this->db->table['pages']).
		$this->db->where("sef_title = '$sef'");
		$this->db->query() or sql_error(__FILE__, __LINE__);
		return $this->db->fetchArray();
	}

	#### ITEM COUNT ############################################################
	function getPageCount() {
		$this->db->count($this->db->table['pages']);
		$this->db->query() or sql_error(__FILE__, __LINE__);
		list($count) = $this->db->fetchRow();
		return $count;
	}

	#### INSERT PAGE ###########################################################
	function insertPage() {
		$this->post['sef_title'] = sef($this->post['title']);
		$this->post['datetime'] = datetime();

		$this->db->insert($this->db->table['pages'], $this->post);
		$this->db->query() or sql_error(__FILE__, __LINE__);
	}

	#### UPDATE PAGE ###########################################################
	function updatePage($id) {
		$this->post['sef_title'] = sef($this->post['title']);
		$this->post['datetime'] = datetime();

		$this->db->update($this->db->table['pages'], $this->post);
		$this->db->where('id='.$id);
		$this->db->query() or sql_error(__FILE__, __LINE__);
	}

	#### DELETE PAGE ###########################################################
	function deletePage($id) {
		$this->db->delete($this->db->table['pages']);
		$this->db->where('id = '.$id);
		$this->db->limit(1);
		$this->db->query() or sql_error(__FILE__, __LINE__);
	}

	#### UPDATE PAGE STATUS ####################################################
	function updatePageStatus($id) {
		$item = $this->getPageByID($id);
		$fields['disabled'] = ($item['disabled'] ? 0 : 1);

		$this->db->update($this->db->table['pages'], $fields);
		$this->db->where('id='.$id);
		$this->db->query() or sql_error(__FILE__, __LINE__);
	}
}
?>